﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Windows.Forms;

namespace PowerPlant
{
    public sealed class DataManager
    {
        public static string getConnectionString()
        {
            return ConfigurationManager.ConnectionStrings["PowerPlantCS"].ToString();
        }
        public static DataTable getDataTableByName()
        {
            DataTable dt = new DataTable();
            string strCon = getConnectionString();
            SqlConnection conn = new SqlConnection(strCon);
            conn.Open();
            string strComm = "select * from dianchang" ;
            SqlDataAdapter sda = new SqlDataAdapter(strComm, conn);
            sda.TableMappings.Add("","");
            sda.Fill(dt);
            conn.Close();
            sda.Dispose();
            dt.Columns["dianchang_id"].ColumnName = "电厂ID";
            dt.Columns["dianchang_name"].ColumnName = "电厂名称";
            dt.Columns["dianchang_capacity"].ColumnName = "装机容量";
            return dt;
        }
        public static DataTable getDataTableByName(string dianchangName)
        {
            PowerPlantEntities PPE = new PowerPlantEntities();
            var model = PPE.dianchang.First(c => c.dianchang_name == dianchangName);
            DataTable dt = new DataTable();
            string strCon = getConnectionString();
            SqlConnection conn = new SqlConnection(strCon);
            conn.Open();
            string strComm = "select * from danji where danji_dianchangId="+model.dianchang_id.ToString();
            SqlDataAdapter sda = new SqlDataAdapter(strComm, conn);
            sda.TableMappings.Add("", "");
            sda.Fill(dt);
            conn.Close();
            sda.Dispose();
            dt.Columns["danji_id"].ColumnName = "单机ID";
            dt.Columns["danji_name"].ColumnName = "单机名称";
            dt.Columns["danji_power"].ColumnName = "单机容量";
            dt.Columns["danji_type"].ColumnName = "单机类型";
            dt.Columns["danji_dianchangId"].ColumnName = "所属电厂";
            return dt;
        }
        public static DataTable getDataTableByName(string danji, string dianchang)
        {
            PowerPlantEntities PPE = new PowerPlantEntities();
            var model = PPE.danji.First(c => c.danji_name == danji);
            DataTable dt = new DataTable();
            string strCon = getConnectionString();
            SqlConnection conn = new SqlConnection(strCon);
            conn.Open();
            string strComm = "select * from status_danji where status_danjiId="+model.danji_id.ToString();
            SqlDataAdapter sda = new SqlDataAdapter(strComm, conn);
            sda.TableMappings.Add("", "");
            sda.Fill(dt);
            conn.Close();
            sda.Dispose();
            dt.Columns["status_id"].ColumnName = "ID";
            dt.Columns["status_fengsu"].ColumnName = "风速";
            dt.Columns["status_power"].ColumnName = "出力";
            dt.Columns["status_danjiId"].ColumnName = "单机ID";
            dt.Columns["status_date"].ColumnName = "测量时间";
            return dt;
        }
    }
}
